package com.scz.util;

import eu.bitwalker.useragentutils.UserAgent;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BrowserInfoUtils {

    /**
     * 从 User-Agent 中获取浏览器信息
     * @return 包含浏览器名称和版本的字符串
     */
    public static String getBrowserInfo() {

        HttpServletRequest request = ServletUtils.getRequest();
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null || userAgent.isEmpty()) {
            return "未知浏览器";
        }

        // 检查 Chrome 浏览器
        if (userAgent.contains("Chrome")) {
            Pattern chromePattern = Pattern.compile("Chrome/(\\d+\\.\\d+)");
            Matcher chromeMatcher = chromePattern.matcher(userAgent);
            if (chromeMatcher.find()) {
                return "Chrome " + chromeMatcher.group(1);
            }
        }

        // 检查 Firefox 浏览器
        if (userAgent.contains("Firefox")) {
            Pattern firefoxPattern = Pattern.compile("Firefox/(\\d+\\.\\d+)");
            Matcher firefoxMatcher = firefoxPattern.matcher(userAgent);
            if (firefoxMatcher.find()) {
                return "Firefox " + firefoxMatcher.group(1);
            }
        }

        // 检查 Safari 浏览器
        if (userAgent.contains("Safari") &&!userAgent.contains("Chrome")) {
            Pattern safariPattern = Pattern.compile("Version/(\\d+\\.\\d+)");
            Matcher safariMatcher = safariPattern.matcher(userAgent);
            if (safariMatcher.find()) {
                return "Safari " + safariMatcher.group(1);
            }
        }

        // 检查 Edge 浏览器
        if (userAgent.contains("Edg")) {
            Pattern edgePattern = Pattern.compile("Edg/(\\d+\\.\\d+)");
            Matcher edgeMatcher = edgePattern.matcher(userAgent);
            if (edgeMatcher.find()) {
                return "Edge " + edgeMatcher.group(1);
            }
        }

        // 检查 IE 浏览器
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            if (userAgent.contains("MSIE")) {
                Pattern iePattern = Pattern.compile("MSIE (\\d+\\.\\d+)");
                Matcher ieMatcher = iePattern.matcher(userAgent);
                if (ieMatcher.find()) {
                    return "IE " + ieMatcher.group(1);
                }
            } else {
                Pattern ie11Pattern = Pattern.compile("rv:(\\d+\\.\\d+)");
                Matcher ie11Matcher = ie11Pattern.matcher(userAgent);
                if (ie11Matcher.find()) {
                    return "IE " + ie11Matcher.group(1);
                }
            }
        }

        return "未知浏览器";
    }

    /**
     * 从 User-Agent 中获取操作系统信息

     * @return 操作系统名称
     */
    public static String getOperatingSystemInfo() {
        HttpServletRequest request = ServletUtils.getRequest();
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null || userAgent.isEmpty()) {
            return "未知操作系统";
        }

        if (userAgent.contains("Windows NT")) {
            if (userAgent.contains("Windows NT 10.0")) {
                return "Windows 10";
            } else if (userAgent.contains("Windows NT 6.3")) {
                return "Windows 8.1";
            } else if (userAgent.contains("Windows NT 6.2")) {
                return "Windows 8";
            } else if (userAgent.contains("Windows NT 6.1")) {
                return "Windows 7";
            } else if (userAgent.contains("Windows NT 6.0")) {
                return "Windows Vista";
            } else if (userAgent.contains("Windows NT 5.1")) {
                return "Windows XP";
            }
        } else if (userAgent.contains("Mac OS X")) {
            return "Mac OS X";
        } else if (userAgent.contains("Linux")) {
            return "Linux";
        } else if (userAgent.contains("Android")) {
            return "Android";
        } else if (userAgent.contains("iPhone") || userAgent.contains("iPad")) {
            return "iOS";
        }

        return "未知操作系统";
    }

    /**
     * 获取登录平台信息
     * @return
     */
    public static String getPlatform() {
        HttpServletRequest request = ServletUtils.getRequest();
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        return userAgent.getOperatingSystem().getDeviceType().getName();
    }

}